package com.nuanshui.heatedloan.credit.service;

import com.nuanshui.framework.consts.TransactionManagerType;
import com.nuanshui.framework.security.DESC;
import com.nuanshui.heatedloan.constant.RabbitMqQueue;
import com.nuanshui.heatedloan.constant.ServiceConstant;
import com.nuanshui.heatedloan.credit.dal.mapper.CustomerAddressListMapper;
import com.nuanshui.heatedloan.credit.facade.CustomerAddressListInfoService;
import com.nuanshui.heatedloan.credit.form.CustomerAddressListInfoForm;
import com.nuanshui.heatedloan.credit.form.CustomerCallRecordListInfoForm;
import com.nuanshui.heatedloan.credit.model.BatchInsertAddressListResult;
import com.nuanshui.heatedloan.credit.vo.CustomerAddressListInfoVO;
import com.nuanshui.heatedloan.middleware.rabbitmq.RabbitmqMessageSender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wangxw on 2017/7/6.
 */
@Service(ServiceConstant.CUSTOMERADDRRESSLISTINFOSERVICE)
@Transactional(readOnly=true,value= TransactionManagerType.CREDIT_TRANSACTION_MANAGER)
public class CustomerAddressListInfoServiceImpl implements CustomerAddressListInfoService {

     @Autowired
     private CustomerAddressListMapper customerAddressListMapper;

    @Autowired
    private RabbitmqMessageSender rabbitmqMessageSender;

    @Override
    public void sendAddressListMsg(List<CustomerAddressListInfoForm> customerAddressListInfos) {
        //放在消息队列中，进行插入，主要考虑到效率问题
        rabbitmqMessageSender.send(RabbitMqQueue.RABBIT_MQ_INSERT_ADDRESS_LIST,customerAddressListInfos,true);
    }

    @Override
    @Transactional(readOnly=false,value= TransactionManagerType.CREDIT_TRANSACTION_MANAGER)
    public void batchInsertAddressList(List<CustomerAddressListInfoForm> customerAddressListInfos){
        for(CustomerAddressListInfoForm customerAddressListInfoForm:customerAddressListInfos){
            String phone= DESC.decrypt3DES(customerAddressListInfoForm.getPhone());
            String name= DESC.decrypt3DES(customerAddressListInfoForm.getName());
            customerAddressListInfoForm.setName(name);
            customerAddressListInfoForm.setPhone(phone);
            customerAddressListInfoForm.setStatus(1);
        }
        customerAddressListMapper.batchInsertAddressList(customerAddressListInfos);
    }

    @Override
    public Map<String,Object> getbatchInsertAddressListResult(String userId, int type){
        BatchInsertAddressListResult batchInsertAddressListResult=customerAddressListMapper.getbatchInsertAddressListResult(userId,type);
        Integer returnValue=0;
        Map<String,Object> maps=new HashMap<String,Object>();
        if(batchInsertAddressListResult==null){
            returnValue=3;
        }else{
            returnValue=batchInsertAddressListResult.getIsSuccess();
            long time=batchInsertAddressListResult.getUpdatedAt().getTime();
            maps.put("time", time);
        }
        maps.put("isSuccess",returnValue);
        return maps;
    }
    @Override
    @Transactional(readOnly=false,value= TransactionManagerType.CREDIT_TRANSACTION_MANAGER)
    public void  insertBatchInsertAddressListResult(BatchInsertAddressListResult batchInsertAddressListResult){
        customerAddressListMapper.insertBatchInsertAddressListResult(batchInsertAddressListResult);
    }

    @Override
    @Transactional(readOnly=false,value= TransactionManagerType.CREDIT_TRANSACTION_MANAGER)
    public void updateBatchInsertAddressListResult(String userId, int type, int isSuccess) {
        customerAddressListMapper.updateBatchInsertAddressListResult(type,isSuccess,userId);
    }

    @Override
    @Transactional(readOnly=false,value= TransactionManagerType.CREDIT_TRANSACTION_MANAGER)
    public void insertBatchInsertAddressListResultService(int type, int isSuccess, String userId) {
        Map<String,Object> returnValues=this.getbatchInsertAddressListResult(userId,type);
        //新增
        if(returnValues.get("isSuccess")==3){
            BatchInsertAddressListResult batchInsertAddressListResult=new BatchInsertAddressListResult();
            batchInsertAddressListResult.setUserId(userId);
            batchInsertAddressListResult.setIsSuccess(isSuccess);
            batchInsertAddressListResult.setStatus(1);
            batchInsertAddressListResult.setType(2);
            this.insertBatchInsertAddressListResult(batchInsertAddressListResult);
        }
        //修改
        else{
            this.updateBatchInsertAddressListResult(userId,type,isSuccess);
        }
    }


    @Override
    public void sendCallRecordListMsg(List<CustomerCallRecordListInfoForm> customerCallRecordListInfoForms) {
        rabbitmqMessageSender.send(RabbitMqQueue.RABBIT_MQ_INSERT_CALL_RECORD_LIST,customerCallRecordListInfoForms,true);
    }

    @Override
    @Transactional(readOnly=false,value= TransactionManagerType.CREDIT_TRANSACTION_MANAGER)
    public void batchInsertCustomerCallRecordList(List<CustomerCallRecordListInfoForm> customerCallRecordListInfos) {
        for(CustomerCallRecordListInfoForm customerCallRecordListInfoForm:customerCallRecordListInfos){
            String name= DESC.decrypt3DES(customerCallRecordListInfoForm.getName());
            String phone=DESC.decrypt3DES(customerCallRecordListInfoForm.getPhone());
            customerCallRecordListInfoForm.setPhone(phone);
            customerCallRecordListInfoForm.setName(name);
        }
        customerAddressListMapper.batchInsertCustomerCallRecordList(customerCallRecordListInfos);
    }

    @Override
    public List<CustomerAddressListInfoVO> loadAddressListVOByUserId(String userId) {
        return customerAddressListMapper.loadAddressListVOByUserId(userId);
    }

    public static void main(String[] args) {
//        Date date=new Date();
//        System.out.println(date.getTime());
        System.out.println(DESC.decrypt3DES("8612v94Xdvk="));
        System.out.println(DESC.decrypt3DES("zir2+/Ssh3fkbpd2bDFQUQ=="));
    }
}
